package in.srain.cube.views.sliderbanner;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;

import in.srain.cube.views.banner.BannerAdapter;
import in.srain.cube.views.banner.SliderBanner;
import in.srain.cube.views.ptr.R;
public class SliderBannerController {
    private SliderBanner mSliderBanner;

    private InnerAdapter mBannerAdapter = new InnerAdapter();
    private OnItemClickListener mItemClickListener;

    public SliderBannerController(SliderBanner sliderBanner) {
        mSliderBanner = sliderBanner;
        sliderBanner.setAdapter(mBannerAdapter);
    }
    public void play(List<String> list) {
        mBannerAdapter.setData(list);
        mBannerAdapter.notifyDataSetChanged();
        mSliderBanner.setDotNum(list.size());
        mSliderBanner.beginPlay();
    }

    public   void  setOnItemClickListener(OnItemClickListener listener){
        this.mItemClickListener=listener;
    }

    private class InnerAdapter extends BannerAdapter {
        private List<String > mDataList;

        public void setData(List<String > data) {
            mDataList = data;
        }

        public String getItem(int position) {
            if (mDataList == null)
                return null;
            return mDataList.get(getPositionForIndicator(position));
        }

        @Override
        public int getPositionForIndicator(int position) {
            if (null == mDataList || mDataList.size()== 0) {
                return 0;
            }
            return position % mDataList.size();
        }

        @Override
        public View getView(LayoutInflater layoutInflater, int position) {
            View convertView = layoutInflater.inflate(R.layout.slider_banner_item, null);

//            JsonData item = getItem(position);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.banner_item_image);
//            imageView.setAdjustViewBounds(false);
//            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

//            String pic = item.optString("pic");
            Glide.with(imageView.getContext())
                    .load(getItem(position))
                    .centerCrop()
                    .into(imageView);
            TextView titleTextView = (TextView) convertView.findViewById(R.id.banner_item_title);
            titleTextView.setVisibility(View.GONE);
//            if(TextUtils.isEmpty(item.optString("title"))){
//                titleTextView.setVisibility(View.GONE);
//            }else{
//                titleTextView.setVisibility(View.VISIBLE);
//            }
//            titleTextView.setText(item.optString("title"));
            convertView.setTag(getItem(position));
            convertView.setOnClickListener(new PageClickListener(mDataList,position));
            return convertView;
        }

        @Override
        public int getCount() {
            if (mDataList == null) {
                return 0;
            }
            return Integer.MAX_VALUE;
        }
    }
      class  PageClickListener implements View.OnClickListener {
        private  List<String> mDataList;
        private int position;

        public PageClickListener(List<String>  dataList, int position) {
            this.mDataList = dataList;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener==null)return;
            mItemClickListener.onItemClick(v,mDataList,getPositionForIndicator(position));
        }

        public int getPositionForIndicator(int position) {
            if (null == mDataList || mDataList.size() == 0) {
                return 0;
            }
            return position % mDataList.size();
        }
    }
}
